/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanosn.framework.core;

import java.util.HashMap;
import java.util.Map;

/**
 * Base class for components.
 *
 * Every component can declare what extension points it provides, as well as
 * what extension points of other components it extends.
 * 
 * @author selkhateeb
 */
public abstract class Component
{
    // <editor-fold defaultstate="collapsed" desc="Private Fields">
    private ComponentManager componentManager;
    private Map<Class<?>, ExtensionPoint> extensionPoints;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructors">
    private Component()
    {
        //Disable the default constructor
    }

    public Component(ComponentManager componentManager) throws Exception
    {
        this.componentManager = componentManager;
        this.componentManager.addComponent(this);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Public Methods">
    /**
     * Sets the extension points for this component
     * @param extensionPoints one or more ExtensionPoint
     */
    public void setExtensionPoints(ExtensionPoint... extensionPoints)
    {
        this.extensionPoints = new HashMap<Class<?>, ExtensionPoint>(extensionPoints.length);
        for (ExtensionPoint extensionPoint : extensionPoints)
        {
            this.extensionPoints.put(extensionPoint.getImplementedInterface(), extensionPoint);
        }
    }

    /**
     * TODOC
     * Pre:
     * Post:
     * @param interfaceClass
     * @return
     */
    public ExtensionPoint getExtensionPoint(Class<?> interfaceClass)
    {
        return this.extensionPoints.get(interfaceClass);
    }

    /**
     * TODOC
     * Pre:
     * Post:
     * @param extensionPoints
     */
    public void addExtensionPoints(ExtensionPoint... extensionPoints)
    {

        for (ExtensionPoint extensionPoint : extensionPoints)
        {
            this.extensionPoints.put(extensionPoint.getImplementedInterface(), extensionPoint);
        }
    }

    /**
     * TODOC
     * Pre:
     * Post:
     * @param extensionPoints
     */
    public void removeExtensionPoints(ExtensionPoint... extensionPoints)
    {

        for (ExtensionPoint extensionPoint : extensionPoints)
        {
            this.extensionPoints.remove(extensionPoint.getImplementedInterface());
        }
    }

    /**
     * TODOC
     * Pre:
     * Post:
     * @return
     */
    public ComponentManager getComponentManager()
    {
        return componentManager;
    }
    // </editor-fold>
}
